const express = require("express");
const path = require("path");
const cookieParser = require("cookie-parser");
// 文件上传
const multer = require("multer");
const upload = multer({ dest:"./upload" });

const isLogin = require("./modules/isLogin");
const check = require("./modules/check");

// 登录路由
const loginRouter = require("./routers/login");
// 用户路由模块
const userRouter = require("./routers/user/");
const userAddRouter = require("./routers/user/add");
const userSetRouter = require("./routers/user/set");
const userSetPasswordRouter = require("./routers/user/setPassword");
const userResetPasswordRouter = require("./routers/user/resetPassword");
const userSetAvatarRouter = require("./routers/user/setAvatar");
const userDelRouter = require("./routers/user/del");
const userFormworkRouter = require("./routers/user/userFormwork");
const userImportRouter = require("./routers/user/import");
// 部门路由模块
const teamRouter = require("./routers/team/");
const teamAddRouter = require("./routers/team/add");
const teamSetRouter = require("./routers/team/set");
// 会议室路由模块
const meetingRoomRouter = require("./routers/meetingRoom/");
const meetingRoomAddRouter = require("./routers/meetingRoom/add");
const meetingRoomSetRouter = require("./routers/meetingRoom/set");

// 静态资源

const app = express();

app.all("*",(req,res,next)=>{
    // cors配置
    // 允许所有来源
    res.header("Access-Control-Allow-Origin","*");
    // 允许的请求方式
    res.header("Access-Control-Allow-Methods","get,post");
  
    res.header("Access-Control-Allow-Headers",'Content-Type, Content-Length, Authorization, Accept, X-Requested-With, token');
    next();
})

// app.set('views', path.join(__dirname, 'views'));
// app.set('view engine', 'ejs');

// 配置响应静态资源的中间件
app.use( express.static("./upload") );
app.use( express.static("./views") );
// 配置解析 application/json 格式数据的中间件
app.use(express.json());
// 配置解析 application/x-www-form-urlencoded 格式数据的中间件
app.use( express.urlencoded({ extended: false} ));
// 配置解析 cookie 数据的中间件
app.use( cookieParser() );
// 配置能接收的文件类型的中间件
app.use( upload.any() );

// app.get("/", require("./routers/index"));

// 登录路由
app.get("/login", loginRouter);

// 导出模板，导入用
app.get("/userformwork", userFormworkRouter);

// 登录验证
app.all("*", isLogin);

app.get("/check", check);

// 用户管理
app.get("/user", userRouter);
app.post("/user/add", userAddRouter);
app.post("/user/set", userSetRouter);
app.post("/user/setpassword", userSetPasswordRouter);
app.post("/user/resetpassword", userResetPasswordRouter);
app.post("/user/del", userDelRouter);
app.post("/user/setavatar", userSetAvatarRouter);
app.post("/user/import", userImportRouter);

// 部门管理
app.get("/team", teamRouter);
app.post("/team/add", teamAddRouter);
app.post("/team/set", teamSetRouter);

// 会议室管理
app.get("/meetingroom", meetingRoomRouter);
app.post("/meetingroom/add", meetingRoomAddRouter);
app.post("/meetingroom/set", meetingRoomSetRouter);

app.listen(3000,()=>{
    console.log(`服务器开启成功，baseURL为：http://localhost:3000`)
});
